#Boa:Dialog:BuscarDescrip

import wx
import consultasBD

def create(parent):
    return BuscarDescrip(parent)

[wxID_BUSCARDESCRIP, wxID_BUSCARDESCRIPINBUSCAR, 
 wxID_BUSCARDESCRIPINPUT_BUSCAR, wxID_BUSCARDESCRIPLISTBOX1, 
 wxID_BUSCARDESCRIPSTATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(5)]

class BuscaProveedor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_BUSCARDESCRIP, name='BuscarDescrip',
              parent=prnt, pos=wx.Point(449, 177), size=wx.Size(383, 418),
              style=wx.DEFAULT_DIALOG_STYLE,
              title= u'Buscar en la descripci\xf3n')
        self.SetClientSize(wx.Size(375, 384))

        self.staticText1 = wx.StaticText(id=wxID_BUSCARDESCRIPSTATICTEXT1,
              label='Buscar que contenga:', name='staticText1', parent=self,
              pos=wx.Point(8, 8), size=wx.Size(88, 24), style=0)

        self.input_buscar = wx.TextCtrl(id=wxID_BUSCARDESCRIPINPUT_BUSCAR,
              name=u'input_buscar', parent=self, pos=wx.Point(88, 8),
              size=wx.Size(184, 21), style=0, value='')

        self.listBox1 = wx.ListBox(choices=[], id=wxID_BUSCARDESCRIPLISTBOX1,
              name='listBox1', parent=self, pos=wx.Point(8, 40),
              size=wx.Size(360, 336), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox,
              id=wxID_BUSCARDESCRIPLISTBOX1)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBox1ListboxDclick,
              id=wxID_BUSCARDESCRIPLISTBOX1)

        self.inbuscar = wx.Button(id=wxID_BUSCARDESCRIPINBUSCAR,
              label=u'Buscar...', name='inbuscar', parent=self,
              pos=wx.Point(288, 8), size=wx.Size(75, 23), style=0)
        self.inbuscar.Bind(wx.EVT_BUTTON, self.OnInbuscarButton,
              id=wxID_BUSCARDESCRIPINBUSCAR)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
    def busca(self,patron):
        self.listBox1.Clear()
        sql = "SELECT * FROM proveedor WHERE nombre LIKE '%%%s%%'" % patron
        resultado = consultasBD.ordenCrudaDict(sql)
                
        for r in resultado:
            id_off = "---------"
            texto = '%s%s%s' % (str(r['idproveedor']), id_off, str(r['nombre']))
            self.listBox1.Append(texto)

    def OnListBox1Listbox(self, event):
        event.Skip()

    def OnListBox1ListboxDclick(self, event):
        item = self.listBox1.GetStringSelection()
        self.id = item.split("-")
        id = [x for x in self.id if x]
        self.id = id[0]
        self.nombre = id[1]
    
        self.Close()
    
    def Muestraid(self):
        return (self.id, self.nombre)

    def OnInbuscarButton(self, event):
        busqueda = self.input_buscar.Value
        self.busca(busqueda)
        
        
